package com.platform.controller;

import com.platform.entity.UserEntity;
import com.platform.entity.UserExchangeEntity;
import com.platform.service.UserExchangeService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.excel.ExcelExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("userExchange")
public class UserExchangeController{
    @Autowired
    private UserExchangeService userExchangeService;
    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<UserExchangeEntity> userExchangeEntities = userExchangeService.queryList(query);
        int total = userExchangeService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(userExchangeEntities, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 导出用户兑换数据
     */
    @RequestMapping("/export")
    public R export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {

        List<UserExchangeEntity> userExchangeEntities = userExchangeService.queryList(params);

        ExcelExport ee = new ExcelExport("用户礼包兑换列表");

        String[] header = new String[]{"用户名", "卡号", "兑换码", "兑换时间","订单号","订单状态","礼包类型名称"};

        List<Map<String, Object>> list = new ArrayList<>();

        if (userExchangeEntities != null && userExchangeEntities.size() != 0) {
            for (UserExchangeEntity userExchangeEntity : userExchangeEntities) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("nickname",userExchangeEntity.getUserEntity().getNickname());
                map.put("cardNumber",userExchangeEntity.getExchangeCodeEntity().getCardNumber());
                map.put("exchangeCode", userExchangeEntity.getExchangeCodeEntity().getExchangeCode());
                map.put("exchangeTime", userExchangeEntity.getExchangeTime());
                map.put("orderSn", userExchangeEntity.getOrderSn());
                if (null !=  userExchangeEntity.getOrderEntity().getOrderStatus()){
                    String orderStatus="";
                    switch (userExchangeEntity.getOrderEntity().getOrderStatus()) {
                        case 0:
                            orderStatus = "等待付款";//order_status_text = "未付款";
                            break;
                        case 201:
                            orderStatus = "待发货";
                            break;
                        case 300:
                            orderStatus = "待收货";
                            break;
                        case 301:
                            orderStatus = "已完成";
                            break;
                        case 200:
                            orderStatus = "已付款";
                            break;
                        case 101:
                            orderStatus = "已取消";
                            break;
                        case 401:
                            orderStatus = "已取消";
                            break;
                        case 402:
                            orderStatus = "已退货";
                            break;
                    }
                    map.put("orderStatus",orderStatus);
                }
                map.put("giftName", userExchangeEntity.getGiftTypeEntity().getGiftName());

                list.add(map);
            }
        }

        ee.addSheetByMap("会员", list, header);
        ee.export(response);
        return R.ok();
    }
}
